package ru.amse.tsyganov.jumleditor.graphicaleditor;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.graphics.Rectangle;

import ru.amse.tsyganov.jumleditor.model.ActivityGraph;
import ru.amse.tsyganov.jumleditor.model.State;
import ru.amse.tsyganov.jumleditor.model.Transition;
import ru.amse.tsyganov.jumleditor.view.Colors;
import ru.amse.tsyganov.jumleditor.view.Cursors;
import ru.amse.tsyganov.jumleditor.view.StateView;
import ru.amse.tsyganov.jumleditor.view.TransitionView;

/*package*/ class AreaSelectionMode extends EditorMode {
	
	private final GraphicalEditor graphicalEditor;
	
	private Rectangle selectingRectangle;
	
	private Rectangle selectingRectangle2;
	
	public AreaSelectionMode(GraphicalEditor graphicalEditor) {
		if (graphicalEditor == null) {
			throw new IllegalArgumentException();
		}
		this.graphicalEditor = graphicalEditor;
	}
	
	@Override
	public void initState() {
		graphicalEditor.getCanvas().setCursor(
				Cursors.getCursorCross());
		
		selectingRectangle = null;
		selectingRectangle2 = null;
	}

	@Override
	public void keyPressed(KeyEvent e) {
	}

	@Override
	public void keyReleased(KeyEvent e) {
	}

	@Override
	public void mouseDoubleClick(MouseEvent e) {
	}

	@Override
	public void mouseDown(MouseEvent e) {
		selectingRectangle = new Rectangle(e.x, e.y, 0, 0);
		selectingRectangle2 = selectingRectangle;
	}

	@Override
	public void mouseMove(MouseEvent e) {
		if (selectingRectangle != null) {
			selectingRectangle.width = e.x - selectingRectangle.x;
			selectingRectangle.height = e.y - selectingRectangle.y;
			
			int w;
			int h;
			int x;
			int y;
			if (selectingRectangle.width > 0) {
				w = selectingRectangle.width;
				x = selectingRectangle.x;
			} else {
				w = -selectingRectangle.width;
				x = selectingRectangle.x - w;
			}
			if (selectingRectangle.height > 0) {
				h = selectingRectangle.height;
				y = selectingRectangle.y;
			} else {
				h = -selectingRectangle.height;
				y = selectingRectangle.y - h;
			}
			selectingRectangle2 = new Rectangle(x, y, w - 1, h - 1);
			
			graphicalEditor.getActivityGraphView().
				getSelectedItems().selectedStatesClear();
			
			graphicalEditor.getActivityGraphView().
				getSelectedItems().selectedTransitionsClear();
			
			ActivityGraph graph = graphicalEditor.
				getActivityGraphView().getModel();
			
			for (State s : graph.getContents()) {
				StateView<? extends State> stateView = graphicalEditor
						.getActivityGraphView().getModelViewMap()
						.getStatesMap().get(s);

				if (stateView.getRectangle().intersects(selectingRectangle2)) {
					graphicalEditor.getActivityGraphView().getSelectedItems()
							.selectedStatesAdd(stateView);
				}

				for (Transition t : s.getStateVertex().getOutgoing()) {
					TransitionView<? extends Transition> transitionView = graphicalEditor
							.getActivityGraphView().getModelViewMap()
							.getTransitionsMap().get(t);

					if (selectingRectangle2.contains(transitionView
							.getStartPoint())
							|| selectingRectangle2.contains(transitionView
									.getEndPoint())) {

						graphicalEditor.getActivityGraphView()
								.getSelectedItems().selectedTransitionsAdd(
										transitionView);
					}
				}
			}
		}
	}

	@Override
	public void mouseUp(MouseEvent e) {
		graphicalEditor.setEditorMode(
				graphicalEditor.getSelectionMode());
	}

	@Override
	public void paintControl(PaintEvent e) {
		if (selectingRectangle2 != null) {
			e.gc.setForeground(Colors.getBlackColor());
			e.gc.setLineStyle(SWT.LINE_DOT);
			e.gc.drawRectangle(selectingRectangle2);
			e.gc.setLineStyle(SWT.LINE_SOLID);
		}
	}
}
